//
//  JMLoginViewController.m
//  JMDome_1
//
//  Created by ios on 15/6/12.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMLoginViewController.h"
#import "JMRegistViewController.h"
#import "JMLoginViewMode.h"
#import "JMForgetViewController.h"

@interface JMLoginViewController ()<UITextFieldDelegate>


@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic,strong) UIImageView *topImageView;
@property (nonatomic, strong) UILabel *topImageLabel;

@property (nonatomic,strong) UIImageView *loginbackImageView;

@property (nonatomic,strong) UITextField *userNameTextField;

@property (nonatomic,strong) UITextField *pwdTextField;

@property (nonatomic,strong) UIButton * forgetBtn;

@property (nonatomic,strong) UIButton *loginBtn;

@property (nonatomic,strong) UIButton *regeistBtn;

@property (nonatomic,strong) UILabel *accountLable;

@property (nonatomic,strong) UILabel *pwdLabel;

@end

@implementation JMLoginViewController

#pragma mark chu shi hua jie mian kong jian
-(UIButton *)creatButton{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    return btn;
}

- (UIImageView *)backImageView
{
    if (!_backImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"login_regist.bundle/back_image"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor clearColor];
        imageView.userInteractionEnabled = YES;
        _backImageView = imageView;
        [self.view addSubview:_backImageView];
    }
    return _backImageView;
}

- (UIImageView *)topImageView{
    if(!_topImageView){
        _topImageView = [[UIImageView alloc]init];
        _topImageView.backgroundColor = [UIColor clearColor];
        _topImageView.image = [UIImage imageNamed:@"login_regist.bundle/topBack_image"];
        [_topImageView addSubview:self.topImageLabel];
        [self.view addSubview:_topImageView];
    }
    return _topImageView;
}
- (UILabel *)topImageLabel
{
    if (!_topImageLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:14.0f];
        label.text = @"账号登陆";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        _topImageLabel = label;
        [self.topImageView addSubview:_topImageLabel];
    }
    return _topImageLabel;
}

-(UIImageView *)loginbackImageView{
    if(!_loginbackImageView){
        _loginbackImageView = [[UIImageView alloc]init];
        _loginbackImageView.backgroundColor = [UIColor clearColor];
        _loginbackImageView.image = [UIImage imageNamed:@"login_regist.bundle/regist_login_back"];
        _loginbackImageView.userInteractionEnabled =YES;
        [self.view addSubview:_loginbackImageView];
    }
    return _loginbackImageView;
}

- (UITextField *)userNameTextField{
    if(!_userNameTextField){
        _userNameTextField = [[UITextField alloc]init];
        _userNameTextField.placeholder =@"请输入手机号";
        _userNameTextField.textColor = [UIColor whiteColor];
//        _userNameTextField.font = [XRTools PXFontConversionIOS:28];
        [_userNameTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        _userNameTextField.delegate =self;
        [self.loginbackImageView addSubview:_userNameTextField];

    }
    return _userNameTextField;
}

- (UITextField *)pwdTextField{
    if(!_pwdTextField){
        _pwdTextField = [[UITextField alloc]init];
        _pwdTextField.backgroundColor = [UIColor clearColor];
        _pwdTextField.placeholder = @"请输入密码";
        _pwdTextField.textColor = [UIColor whiteColor];
        [_pwdTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        _pwdTextField.delegate =self;
//        _pwdTextField.font = [XRTools PXFontConversionIOS:28];
        [self.loginbackImageView addSubview:_pwdTextField];
    }
    return _pwdTextField;
}

- (UIButton *)regeistBtn{
    if(!_regeistBtn){
        _regeistBtn = [self creatButton];
        [_regeistBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_regeistBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //        _regeistBtn.titleLabel.font = [XRTools PXFontConversionIOS:24];
        [_regeistBtn addTarget:self action:@selector(registButtonClcik:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_regeistBtn];
    }
    return _regeistBtn;
}

- (UIButton *)forgetBtn{
    if(!_forgetBtn){
        _forgetBtn = [self creatButton];
        [_forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
        //        _forgetBtn.titleLabel.font = [XRTools PXFontConversionIOS:24];
        [_forgetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_forgetBtn addTarget:self action:@selector(forgetButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _forgetBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _forgetBtn.layer.borderColor = [[UIColor whiteColor] CGColor];
        _forgetBtn.layer.borderWidth = 1.0;
        _forgetBtn.layer.masksToBounds = YES;
        _forgetBtn.layer.cornerRadius = 3;
        [self.loginbackImageView addSubview:_forgetBtn];
    }
    return _forgetBtn;
}

- (UIButton *)loginBtn{
    if(!_loginBtn){
        _loginBtn = [self creatButton];
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"login_regist.bundle/seleted_backImage"] forState:UIControlStateNormal];
        _loginBtn.backgroundColor = [UIColor clearColor];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_loginBtn addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.loginbackImageView addSubview:_loginBtn];
    }
    return _loginBtn;
}

- (UILabel *)accountLable{
    if(!_accountLable){
        _accountLable = [[UILabel alloc]init];
        _accountLable.backgroundColor = [UIColor clearColor];
        _accountLable.text = @"账号";
        _accountLable.textColor = [UIColor whiteColor];
        _accountLable.font = [UIFont systemFontOfSize:18];
        [self.loginbackImageView addSubview:_accountLable];
    }
    return _accountLable;
}

- (UILabel *)pwdLabel{
    if(!_pwdLabel){
        _pwdLabel = [[UILabel alloc]init];
        _pwdLabel.backgroundColor = [UIColor clearColor];
        _pwdLabel.text = @"密码";
        _pwdLabel.textColor = [UIColor whiteColor];
        _pwdLabel.font = [UIFont systemFontOfSize:18.0];
        [self.loginbackImageView addSubview:_pwdLabel];
    }
    return _pwdLabel;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
    self.navBackBtn.hidden =YES;
    
    [[UITextField appearance] setTintColor:[UIColor whiteColor]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHidden:) name:UIKeyboardDidHideNotification object:nil];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.backImageView.frame = self.view.bounds;
    self.topImageView.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.25625/2.0, 26, UI_SCREEN_WITH *0.25625, UI_SCREEN_WITH *0.0875);
    self.topImageLabel.frame = CGRectMake(0, 0, self.topImageView.frameWidth, self.topImageView.frameHeight);
    self.regeistBtn.frame = CGRectMake(UI_SCREEN_WITH - 60, 22, 60, 40);
    
    CGFloat login ;
    if(iphone4){
        login = 60;
    }else if (iphone5){
        login = 100;
    }else if(iphone6){
        login = 120;
    }else{
        login = 140;
    }
    self.loginbackImageView.frame = CGRectMake(0, login, UI_SCREEN_WITH, UI_SCREEN_WITH *0.671875);
    
    self.accountLable.frame = CGRectMake(UI_SCREEN_WITH *0.09375, UI_SCREEN_WITH *0.171875, UI_SCREEN_WITH *0.125, UI_SCREEN_WITH *0.125);
    self.userNameTextField.frame = CGRectMake(self.accountLable.frameRight + 10, self.accountLable.frameTop, UI_SCREEN_WITH - self.accountLable.frameRight - 110, self.accountLable.frameHeight);
    
    self.pwdLabel.frame = CGRectMake(self.accountLable.frameLeft, self.accountLable.frameBottom, self.accountLable.frameWidth, self.accountLable.frameHeight);
    self.pwdTextField.frame = CGRectMake(self.pwdLabel.frameRight + 10, self.pwdLabel.frameTop, UI_SCREEN_WITH - self.pwdLabel.frameRight - 110, self.pwdLabel.frameHeight);
    self.forgetBtn.frame = CGRectMake(UI_SCREEN_WITH - UI_SCREEN_WITH *0.2735, UI_SCREEN_WITH *0.3359, UI_SCREEN_WITH *0.1954, UI_SCREEN_WITH * 0.0625);
    self.loginBtn.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.234375/2.0, UI_SCREEN_WITH *0.49, UI_SCREEN_WITH *0.234375, UI_SCREEN_WITH *0.07031);
    
    [self.userNameTextField becomeFirstResponder];
    [self.pwdTextField becomeFirstResponder];
    
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
    [self.view addGestureRecognizer:singleRecognizer];
}
-(void)SingleTap:(id)sender{
    [self.userNameTextField resignFirstResponder];
    [self.pwdTextField resignFirstResponder];
}
#pragma mark  注册按钮点击
-(void)registButtonClcik:(id)sender{
    JMRegistViewController *vc =[JMRegistViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark  忘记密码按钮点击
-(void)forgetButtonClick:(id)sender{
    JMForgetViewController *vc =[JMForgetViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark  登陆按钮点击
-(void)loginButtonClick:(id)sender{
    if(![JMTools isiPhoneNumber:self.userNameTextField.text]){
        [JMTools showAlertView:self.view message:@"你输入的不是手机号"];
        return;
    }
    if(![JMTools isRightpwd:self.pwdTextField.text]){
        [JMTools showAlertView:self.view message:@"你输入的密码有误"];
        return;
    }
    [JMTools showAlertView:self.view];
    NSMutableDictionary *dict =[[NSMutableDictionary alloc]initWithCapacity:0];
    [dict setObject:self.userNameTextField.text forKeyedSubscript:@"mobile"];
    [dict setObject:[[NSString stringWithFormat:@"%@%@",JMMd5Key,self.pwdTextField.text] JMMD5String] forKeyedSubscript:@"password"];

    [JMLoginViewMode fetchLoginMessageParametes:dict block:^(id date, NSError *error,id currData) {
        [JMTools hiddenAlerView:self.view];

        if([date[@"status"]integerValue] == 1){
            [JMTools showAlertView:self.view message:date[@"message"]];
            [self dismissViewControllerAnimated:YES completion:^{
            }];
        }else{
            [JMTools showAlertView:self.view message:date[@"message"]];
        }
    }];
}

#pragma mark  jie mian shang xiang guan kong jian de dai li fang fa 


#pragma mark -- jian pan huo dong
-(void)keyBoardShow:(NSNotification *)notion{
    NSDictionary *info = [notion userInfo];
    NSNumber *timeValue = info[UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[timeValue floatValue] animations:^{
//        self.loginbackImageView.frame = CGRectMake(viewMode.backView.frame.origin.x, -_VIEW_HEIGH(100), viewMode.backView.frame.size.width, viewMode.backView.frame.size.height);
    } completion:^(BOOL finished) {
        
    }];
}

-(void)keyBoardHidden:(NSNotification *)notion{
    NSDictionary *info = [notion userInfo];
    NSNumber *timeValue = info[UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[timeValue floatValue] animations:^{
//        viewMode.backView.frame = CGRectMake(viewMode.backView.frame.origin.x, 0, viewMode.backView.frame.size.width, viewMode.backView.frame.size.height);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark  重写返回方法
-(void)backBtnClick:(id)sender{
    
    [self.userNameTextField resignFirstResponder ];
    [self.pwdTextField resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
@end


